<?php

namespace App\Http\Controllers\Api;

use App\Models\FlowCollection;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;

class FlowCountController extends AppBaseController
{

    /**
     * 根据request构建查询条件
     *
     * @param $request
     * @param $parks
     * @return FlowCollection
     */
    private function queryBuild($request,$parks)
    {
        $model = new FlowCollection();


        if($request->get('park_code')) {
            $model = $model->whereIn('park_code',$request->park_code);
        }else{
            $model = $model->whereIn('park_code',$parks);
        }

        if($request->get('start_time')) {
            $model = $model->where('hour', '>=' ,$request->get('start_time'));
        }

        if($request->get('end_time')) {
            $model = $model->where('hour', '<' ,$request->get('end_time'));
        }

        return $model;
    }


    /**
     * 查询字段定义
     *
     * @return array
     */
    private function selectField()

    {
        return [
            \DB::raw('IFNULL(SUM(leave_flow),0)as leave_flow'),
            \DB::raw('IFNULL(SUM(short_time_free),0) as short_time_free'),
            \DB::raw('IFNULL(SUM(self_free),0) as self_free'),
            \DB::raw('IFNULL(SUM(inner_car),0) as inner_car'),
            \DB::raw('IFNULL(SUM(free_car),0) as free_car'),
            \DB::raw('IFNULL(SUM(blank_car),0) as blank_car'),
            \DB::raw('IFNULL(SUM(worker_car),0) as worker_car'),
            \DB::raw('IFNULL(SUM(other_free),0) as other_free'),
            \DB::raw('IFNULL(SUM(free_car_total),0) as free_car_total'),
            \DB::raw('IFNULL(SUM(big_car),0) as big_car'),
            \DB::raw('IFNULL(SUM(taxi),0) as taxi'),
            \DB::raw('IFNULL(SUM(nev),0) as nev'),
            \DB::raw('IFNULL(SUM(small_car),0) as small_car'),
            \DB::raw('IFNULL(SUM(other_charge_car),0) as other_charge_car'),
            \DB::raw('IFNULL(SUM(total_charge_car),0) as total_charge_car'),
        ];
    }

    /**
     * 查询车流量方法
     *
     * @param $request
     * @return static
     */
    private function searchFlow($request)
    {
        $parks = $this->getUserParks();
        $parkCode = $this->getParkCodes($parks);
        $total = $this->queryBuild($request, $parkCode)->select($this->selectField())->first()->toArray();

        $res = $this->queryBuild($request, $parkCode)->groupBy('park_code')
            ->select(array_merge(['park_code'], $this->selectField()))->get();

        //根据传的park_code排序
        $newRes=collect([]);
        foreach ($request->park_code as $v){
            $item = $this->queryBuild($request, $parkCode)->groupBy('park_code')
                ->select(array_merge(['park_code'], $this->selectField()))->where('park_code',$v)->get();
            if($item->count() > 0){
                $newRes->push($item->first());
            }
        }

        $data=array_merge([$total],$newRes->toArray());

        $response = collect($data)->map(function($item,$key) use ($parks){
            if($key==0){
                $item['name']='合计';
            }else{
                $code = $item['park_code'];

                $item['name']=$parks->has($code) ? $parks[$code] : '';
            }

            return $item;
        });

        return $response;
    }


    /**
     * 查询车流量方法
     *
     * @param $request
     * @return static
     */
    private function searchFlowByDay($request)
    {
        $parks = $this->getUserParks();
        $parkCode = $this->getParkCodes($parks);

        $data = [];
        $list = $this->queryBuild($request, $parkCode)->get();

        foreach ($list as $item) {
            $tmp = [
                'leave_flow' => intval($item->leave_flow),
                'short_time_free' => intval($item->short_time_free),
                'self_free' => intval($item->self_free),
                'worker_car' => intval($item->worker_car),
                'free_car' => intval($item->free_car),
                'blank_car' => intval($item->blank_car),
                'other_free' => intval($item->other_free),
                'free_car_total' => intval($item->free_car_total),
                'big_car' => intval($item->big_car),
                'taxi' => intval($item->taxi),
                'nev' => intval($item->nev),
                'small_car' => intval($item->small_car),
                'other_charge_car' => intval($item->other_charge_car),
                'total_charge_car' => intval($item->total_charge_car),
                'inner_car' => intval($item->inner_car),
                'etc_charge_car' => intval($item->etc_charge_car),
                'etc_free_car' => intval($item->etc_free_car),
                'etc_small_car' => intval($item->etc_small_car),
                'etc_big_car' => intval($item->etc_big_car),
            ];

            $date = substr($item->hour, 0, 10);
            if (!isset($data[$date])) {
                $data[$date] = $tmp;
            } else {
                foreach ($tmp as $k => $v) {
                    $data[$date][$k] += $v;
                }
            }
            $data[$date]['date'] = $date;
        }

        return array_values($data);
    }

    /**
     *查询车流接口
     *
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $response = $this->searchFlow($request);

        return $this->sendResponse($response->toArray(),'请求成功');

    }

    /**
     *查询车流接口
     *
     * @param Request $request
     * @return mixed
     */
    public function flowByDay(Request $request)
    {
        $response = $this->searchFlowByDay($request);

        return $this->sendResponse($response,'请求成功');

    }

}
